const response = require("../../utils/response.js");
const validate = require("../../utils/validate.js");
const messageService = require("../services/messageService.js");
const { URLSearchParams } = require('url');
const { deleteMessageRules, updateMessageRules, createMessageRules, getMessageByIdRules, userCreateMessageRules } = require("../validations/messageValidation.js");

class MessageController {
    async userAddMessage(ctx) {
        try {

            const { content } = await  validate(ctx, userCreateMessageRules);

            const { id } = await messageService.addMessage({
                content,
                userId: ctx.state.user.id
            });
            if (!id > 0) throw '添加失败';
    
            response.success(ctx, null);
    
        } catch (err) {
            console.error(err);
            response.error(ctx, err);
        }
    }
    async addMessage(ctx) {
        try {

            const messageData = await  validate(ctx, createMessageRules);

            const { id } = await messageService.addMessage(messageData);
            if (!id > 0) throw '添加失败';
    
            response.success(ctx, null);
    
        } catch (err) {
            console.error(err);
            response.error(ctx, err);
        }
    }
    async deleteMessage(ctx) {
        try {
            const { id } = await validate(ctx, deleteMessageRules);
            const messageInstance = await messageService.getMessageById(id);
            if(!messageInstance) throw '没找到对应记录';

            await messageInstance.destroy();

            response.success(ctx, id, '删除成功')
        } catch (err) {
            console.log(err)
            response.error(ctx, err);
        }
    }
    async updateMessage(ctx) {
        try {
            const messageData = await validate(ctx, updateMessageRules);
            await messageService.updateMessage(messageData);
            response.success(ctx, '修改成功')
        } catch (err) {
            console.log(err)
            response.error(ctx, err);
        }
    }
    async getMessageListByPage(ctx) {
        const usp = new URLSearchParams(ctx.querystring);
        let page = Number(usp.get('page')) || 1;
        let limit = Number(usp.get('limit')) || 10;
        let keyword = usp.get('keyword') || '';
        let sort = usp.get('sort') || 'DESC';
        try {
            const { rows, count } = await messageService.getMessageListByPage(page, limit, keyword, sort);
            response.success(ctx, {
                page,
                limit,
                rows,
                total: count
            });
        } catch (err) {
            console.log(err)
            response.error(ctx, err);
        }
    }
    async getAllMessages(ctx) {
        try {
            const rows = await messageService.getAllMessages();
            response.success(ctx, rows);
        } catch (err) {
            console.log(err, 'err')
            response.error(ctx, err);
        }
    }
    async getMessageById(ctx){
        try{
            const { id } = await validate(ctx, getMessageByIdRules);
            const userData = await messageService.getMessageById(id);
            if(!userData) throw '没找到对应记录';
            userData.dataValues.password = null
            response.success(ctx, userData);
        }catch(err){
            console.log(err, 'getMessageById');
            response.error(ctx, err);
        }
    }
}


module.exports = new MessageController();